package com.zimi.util;

import org.springframework.transaction.annotation.Transactional;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

@Transactional
public class YeWuSaveUpdate {
    //保存数据
    public static Integer sendData(Map<String, Object> map, String className) throws Exception {
        Integer id = null;
        //根据类名创建对象
        //根据类名创建对象
        Class<?> clazz = Class.forName(className);
        Object obj = clazz.newInstance();
        //给对象的属性赋值
        Field[] fields = clazz.getDeclaredFields();
        for (Field f : fields) {
            if (map.containsKey(f.getName()) && map.get(f.getName()) != null && !"".equals((map.get(f.getName())).toString())) {
                Object val = map.get(f.getName());
                String vals = val.toString();
                Object objType = f.getType();

                PropertyDescriptor pd = null;
                pd = new PropertyDescriptor(f.getName(), clazz);
                //获得set方法
                Method wM = pd.getWriteMethod();

                //判断数据类型
                if (!((Class) objType).getName().equals("java.lang.String")) {
                    if (((Class) objType).getName().equals("java.lang.Integer")) {
                        wM.invoke(obj, Integer.parseInt(vals));
                    } else if(((Class) objType).getName().equals("java.lang.Double")){
                        wM.invoke(obj, Double.parseDouble(vals));
                    } else if (((Class) objType).getName().equals("java.time.LocalDateTime")) {
                        wM.invoke(obj, LocalDateTime.parse(vals));
                    } else if (((Class) objType).getName().equals("java.util.Date")) {
                        Date time;
                        try {
                            if (vals.indexOf("T") == -1 && vals.length() > 10) {
                                DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                time = format.parse(vals);
                            } else if (vals.indexOf("T") == -1 && vals.length() == 10) {
                                DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                                time = format.parse(vals);
                            } else {
                                DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                                time = format.parse(vals);
                            }
                        } catch (ParseException e) {
                            time = new Date(vals);
                        }
                        wM.invoke(obj, time);
                    } else if (((Class) objType).getName().equals("java.math.BigDecimal")) {
                        wM.invoke(obj, new BigDecimal(vals));
                    }
                } else {
                    wM.invoke(obj, vals);
                }
            }
        }

        Method method = null;
        method = clazz.getMethod("insert");
        method.invoke(obj);

        PropertyDescriptor pd = null;
        pd = new PropertyDescriptor("id", clazz);
        Method method1 = pd.getReadMethod();
        Object objId = method1.invoke(obj);
        if (objId != null && !"".equals(objId)) {
            id = Integer.parseInt(method1.invoke(obj).toString());
        }
        return id;
    }

    //修改数据
    public static void updateData(Map<String, Object> map, String className) {
        //根据类名创建对象
        try {
            //根据类名创建对象
            Class clazz = Class.forName(className);
            Object obj = clazz.newInstance();
            //给对象的属性赋值
            Field[] fields = clazz.getDeclaredFields();
            for (Field f : fields) {
                if (map.containsKey(f.getName()) && map.get(f.getName()) != null && !"".equals((map.get(f.getName())).toString())) {
                    String vals = map.get(f.getName()).toString();
                    Object objType = f.getType();

                    PropertyDescriptor pd = null;
                    pd = new PropertyDescriptor(f.getName(), clazz);
                    //获得set方法
                    Method wM = pd.getWriteMethod();

                    //判断数据类型
                    if (!((Class) objType).getName().equals("java.lang.String")) {
                        if (((Class) objType).getName().equals("java.lang.Integer")) {
                            wM.invoke(obj, Integer.parseInt(vals));
                        } else if(((Class) objType).getName().equals("java.lang.Double")){
                            wM.invoke(obj, Double.parseDouble(vals));
                        } else if (((Class) objType).getName().equals("java.time.LocalDateTime")) {
                            wM.invoke(obj, LocalDateTime.parse(vals));
                        } else if (((Class) objType).getName().equals("java.util.Date")) {
                            Date time = null;
                            try {
                                DateFormat format;
                                if (vals.indexOf("T") == -1 && vals.length() > 10) {
                                    format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                } else if (vals.indexOf("T") == -1 && vals.length() <= 10) {
                                    format = new SimpleDateFormat("yyyy-MM-dd");
                                } else {
                                    format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                                }
                                time = format.parse(vals);
                            } catch (ParseException e) {
                                time = new Date(vals);
                            }
                            wM.invoke(obj, time);
                        } else if (((Class) objType).getName().equals("java.math.BigDecimal")) {
                            wM.invoke(obj, new BigDecimal(vals));
                        }
                    } else {
                        wM.invoke(obj, vals);
                    }
                }
            }

            Method method = null;
            method = clazz.getMethod("updateById");
            method.invoke(obj);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

}
